package com.ym.controller.doctor.contacts;

import com.ym.common.annotations.CustomConstraint;
import com.ym.common.constant.Constants;
import com.ym.core.annotation.Log;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.shiro.ShiroUtil;
import com.ym.medical.domain.entity.AddressGroupEntity;
import com.ym.medical.domain.vo.AddressBookPacketDataVo;
import com.ym.medical.enums.AddressGroupAlgIdentEnum;
import com.ym.medical.enums.AddressGroupAlgTypeEnum;
import com.ym.medical.service.IAddressGroupSV;
import com.ym.medical.service.IAddressListSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.util.List;

/**
 * 通讯录首页相关接口
 *
 * @author: 李涛
 * @version: 2019年05月22日 10:23
 */
@Api(tags = "医生端-通讯录-首页分组")
@RestController
@RequestMapping(value = "/doctor/contacts/index", headers = Constants.Api.VERSION)
@Validated
public class ContactsIndexController extends BaseController {

	@Autowired
	private IAddressGroupSV addressGroupSV;

	@Autowired
	private IAddressListSV addressListSV;

	@Log(dataBase = false, printBody = false)
	@ApiOperation(value = "获取当前用户的分组列表数据", notes = "患者/医生/消息共用")
	@GetMapping("/group/info")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "type", value = "类型:患者/医生/消息.KEY=AddressGroupAlgIdentEnum", required = true, paramType = "query"),
	})
	public APIResponse<List<AddressBookPacketDataVo>> groupInfo(
			@CustomConstraint(value = AddressGroupAlgIdentEnum.class) String type
	) {
		List<AddressBookPacketDataVo> data = addressGroupSV.getAddressBookPacketData(ShiroUtil.getLoginId(), type);
		return successData(data);
	}

	@Log
	@ApiOperation(value = "删除分组", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "分组ID", required = true, paramType = "form"),
	})
	@PostMapping(value = "/group/del", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	public APIResponse groupDel(
			@NotNull Long id
	) {
		addressGroupSV.deleteById(id);
		return successMsg("操作成功");
	}


	@Log
	@ApiOperation("新增分组")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "type", value = "类型: 患者/医生/消息,KEY=AddressGroupAlgIdentEnum", required = true, paramType = "form"),
			@ApiImplicitParam(name = "name", value = "分组名称", required = true, paramType = "form"),
	})
	@PostMapping(value = "/group/add", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	public APIResponse groupAdd(
			@NotEmpty @CustomConstraint(value = AddressGroupAlgIdentEnum.class) String type,
			@NotEmpty @Pattern(regexp = "^.{1,50}$", message = "名称过长!") String name
	) {
		AddressGroupEntity addressGroup = new AddressGroupEntity();
		addressGroup.setUserId(ShiroUtil.getLoginId());
		addressGroup.setAlgIdent(type);
		addressGroup.setAlgType(AddressGroupAlgTypeEnum.THE_USER.getKey());
		addressGroup.setAlgName(name);
		addressGroupSV.insert(addressGroup);
		return successMsg("操作成功");
	}

	@Log
	@ApiOperation("扫码后添加好友到通讯录")
	@PostMapping(value = "/addFriend", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "添加ID:可能是用户ID/患者ID", required = true, paramType = "form"),
			@ApiImplicitParam(name = "idType", value = "添加类型:01用户/02医生/03患者", required = true, paramType = "form"),
			@ApiImplicitParam(name = "groupId", value = "分组ID", required = false, paramType = "form"),
	})
	public APIResponse addFriend(
			@NotNull Long id,
			@NotEmpty @Pattern(regexp = "^01|02|03$", message = "参数类型不匹配") String idType,
			Long groupId
	) throws Exception {
		addressListSV.addFriend(id, ShiroUtil.getLoginId(), idType, groupId);
		return successMsg("添加成功!");
	}


	@Log
	@ApiOperation("屏蔽患者")
	@PostMapping(value = "/shieldFriend", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "添加ID:可能是用户ID/患者ID", required = true, paramType = "form"),
			@ApiImplicitParam(name = "idType", value = "添加类型:01用户/02医生/03患者", required = true, paramType = "form"),
			@ApiImplicitParam(name = "groupId", value = "分组ID", required = false, paramType = "form"),
			@ApiImplicitParam(name = "type", value = "01 屏蔽 02 移出", required = true, paramType = "form"),
	})
	public APIResponse shieldFriend(
			@NotNull Long id,
			@NotEmpty @Pattern(regexp = "^01|02|03$", message = "参数类型不匹配") String idType,
			Long groupId,
			@NotEmpty @Pattern(regexp = "^01|02$", message = "参数类型不匹配") String type) throws Exception {
		int rows = addressListSV.shieldFriend(id, ShiroUtil.getLoginId(), idType, groupId, type);
		if (rows == 0) {
			return failMsg("屏蔽失败");
		}
		return successMsg("成功!");
	}


}
